package com.summer.peony.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.system.mapper.SysRoleMapper;
import com.summer.peony.system.mapper.SysRoleMenuMapper;
import com.summer.peony.system.pojo.SysRole;
import com.summer.peony.system.pojo.SysRoleExample;
import com.summer.peony.system.pojo.SysRoleExample.Criteria;
import com.summer.peony.system.pojo.SysRoleMenuExample;
import com.summer.peony.system.pojo.SysRoleMenuKey;
import com.summer.peony.system.service.SysRoleService;

/**
 * @ClassName: SysRoleServiceImpl
 * @Description: TODO(角色管理业务层)
 * @author: TaoGe
 * @date: 2020年6月30日 下午12:23:49
 * @version: v0.0.1
 */
@Transactional
@Service
public class SysRoleServiceImpl implements SysRoleService {

	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Override
	public AjaxResult insert(SysRole sysRole) {
		// TODO 新增角色
		Date date = new Date();
		sysRole.setCreateTime(date);
		sysRole.setUpdateTime(date);
		sysRole.setDelFlag("0");
		SysRoleExample example = new SysRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleCodeEqualTo(sysRole.getRoleCode());
		List<SysRole> list = sysRoleMapper.selectByExample(example);
		AjaxResult ajaxResult = new AjaxResult();
		if (list.size() > 0) {
			ajaxResult.put("code", 900006);
			ajaxResult.put("msg", "角色编码已存在");
			return ajaxResult;
		}
		int insert = sysRoleMapper.insert(sysRole);
		int insertRoleMenu = insertRoleMenu(sysRole);
		if (insert == 1 && insertRoleMenu > 0) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "添加成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 999999);
		ajaxResult.put("msg", "添加失败");
		return ajaxResult;
	}

	@Override
	public Integer deleteByPrimaryKey(Integer roleId) {
		// TODO 删除角色
		SysRole sysRole = sysRoleMapper.selectByPrimaryKey(roleId);
		sysRole.setDelFlag("1");
		return sysRoleMapper.updateByPrimaryKeySelective(sysRole);
	}

	@Override
	public AjaxResult updateByPrimaryKey(SysRole sysRole) {
		// TODO 修改角色
		SysRole sysRole2 = sysRoleMapper.selectByPrimaryKey(sysRole.getRoleId());
		Date date = new Date();
		sysRole2.setUpdateTime(date);
		sysRole2.setRoleDesc(sysRole.getRoleDesc());
		sysRole2.setRoleName(sysRole.getRoleName());
		sysRole2.setMenuIds(sysRole.getMenuIds());
		SysRoleExample example = new SysRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleIdNotEqualTo(sysRole.getRoleId()).andRoleCodeEqualTo(sysRole.getRoleCode());
		List<SysRole> list = sysRoleMapper.selectByExample(example);
		AjaxResult ajaxResult = new AjaxResult();
		if (list.size() > 0) {
			ajaxResult.put("code", 900006);
			ajaxResult.put("msg", "角色编码已存在");
			return ajaxResult;
		}
		sysRole2.setRoleCode(sysRole.getRoleCode());
		int keySelective = sysRoleMapper.updateByPrimaryKeySelective(sysRole2);
		sysRoleMenuMapper.deleteRoleMenuByRoleId(sysRole2.getRoleId());
		int insertRoleMenu = insertRoleMenu(sysRole2);
		if (keySelective == 1 && insertRoleMenu > 0) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "修改成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 999999);
		ajaxResult.put("msg", "修改失败");
		return ajaxResult;
	}

	@Override
	public PageInfo<SysRole> selectAllRoles(String roleName, String roleCode, Integer pageNum, Integer pageSize) {
		// TODO 分页查询角色列表
		SysRoleExample example = new SysRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagNotEqualTo("1").andRoleNameLike("%" + roleName + "%").andRoleCodeLike("%" + roleCode + "%");
		PageHelper.startPage(pageNum, pageSize);
		List<SysRole> list = sysRoleMapper.selectByExample(example);
		for (SysRole sysRole : list) {
			SysRoleMenuExample example2 = new SysRoleMenuExample();
			com.summer.peony.system.pojo.SysRoleMenuExample.Criteria criteria2 = example2.createCriteria();
			criteria2.andRoleIdEqualTo(sysRole.getRoleId());
			List<SysRoleMenuKey> list2 = sysRoleMenuMapper.selectByExample(example2);
			if (!list2.isEmpty()) {
				sysRole.setMenuIds(sysRoleMenuMapper.getAllMenuIds(sysRole.getRoleId()));
			}
		}
		PageInfo<SysRole> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public List<SysRole> selectRolelist() {
		// TODO 根据角色列表
		SysRoleExample example = new SysRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0");
		List<SysRole> list = sysRoleMapper.selectByExample(example);
		for (SysRole sysRole : list) {
			SysRoleMenuExample example2 = new SysRoleMenuExample();
			com.summer.peony.system.pojo.SysRoleMenuExample.Criteria criteria2 = example2.createCriteria();
			criteria2.andRoleIdEqualTo(sysRole.getRoleId());
			List<SysRoleMenuKey> list2 = sysRoleMenuMapper.selectByExample(example2);
			if (!list2.isEmpty()) {
				sysRole.setMenuIds(sysRoleMenuMapper.getAllMenuIds(sysRole.getRoleId()));
			}
		}
		return list;
	}

	@Override
	public SysRole selectByPrimaryKey(Integer roleId) {
		// TODO 查询单个角色
		return sysRoleMapper.selectByPrimaryKey(roleId);
	}

	/**
	 * @Description: TODO(新增角色权限)
	 * @param sysRole
	 * @return int
	 * @author: TaoGe
	 * @date: 2021年1月27日 下午4:55:43
	 * @throws:
	 */
	public int insertRoleMenu(SysRole sysRole) {
		int rows = 1;
		SysRoleExample example = new SysRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleCodeEqualTo(sysRole.getRoleCode());
		List<SysRole> list2 = sysRoleMapper.selectByExample(example);
		if (list2.isEmpty()) {
			return 2;
		}
		Integer roleId = list2.get(0).getRoleId();
		// 新增用户与角色管理
		List<SysRoleMenuKey> list = new ArrayList<SysRoleMenuKey>();
		for (Integer menuId : sysRole.getMenuIds()) {
			SysRoleMenuKey rm = new SysRoleMenuKey();
			rm.setRoleId(roleId);
			rm.setMenuId(menuId);
			list.add(rm);
		}
		if (list.size() > 0) {
			rows = sysRoleMenuMapper.batchRoleMenu(list);
		}
		return rows;
	}

}